package com.mnsq.common.controller;

import com.mnsq.common.bean.condition.DocumentCondition;
import com.mnsq.common.service.MongoService;
import com.vwkit.common.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @author 周俭
 */
public class BaseMongoController<T, C extends DocumentCondition> {

    @Autowired
    private MongoService<T> mongoService;

    @PostMapping("execute")
    public Result execute(@RequestBody C documentCondition) {
        DocumentCondition.Type type = documentCondition.getType();
        switch (type) {
            case PAGE:
                return this.page(documentCondition);
            case LIST:
                break;
            case DETAIL:
                return this.detail(documentCondition);
            case UPDATE:
                return this.update(documentCondition);
            default:
                throw new IllegalArgumentException("不支持的执行方法");
        }
        return null;
    }

    private Result page(C documentCondition) {
        documentCondition.pageValid();
        return Result.success(mongoService.page(documentCondition));
    }

    public Result detail(C documentCondition) {
        documentCondition.detailValid();
        return Result.success(mongoService.detail(documentCondition));
    }

    public Result update(C documentCondition) {
        documentCondition.updateValid();
        mongoService.update(documentCondition);
        return Result.success(true);
    }
}
